//获取应用实例
const app = getApp();
var WxParse = require('../../wxParse/html2json.js');
Page({
  data: {
    carts: [],               // 购物车列表
    hasList: true,          // 列表是否有数据
    totalPrice: 0,           // 总价，初始为0
    selectAllStatus: false,   
    obj: {
      name: "hello"
    },
    ids:'',  //id 拼接字符串
    number:"",//number拼接字符串
  },

  getCartList:function(){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        let openid = res.data
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.globalData.URL + '/app/product/shoppingCart?openid=' + openid,
          success: function (res) {
            console.log("返回数据", res)
            wx.hideLoading()
            that.setData({
              carts: res.data.qhcPro || [],
              paseImg: that.parseImg(res.data.qhcPro, that)
            })
            console.log("carts", that.data.carts)
            
            if (that.data.carts.length > 0) {
              that.setData({
                hasList: true,
              })
            }else{
              that.setData({
                hasList: false,
              })
            }
          }

        })
      },
    })
  },

  onShow() {  
    this.getCartList()    
    this.getTotalPrice();
  },

  //解析富文本
  parseImg: function (data, that) {
    var parseImgObj = {}
    for (var i = 0; i < data.length; i++) {
      parseImgObj[data[i].shoppingId] = WxParse.html2json(data[i]['productClassPic'], 'returnData');
    }
    return parseImgObj
  },
  /**
   * 当前商品选中事件
   */
  selectList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    const selected = carts[index].selected;
    carts[index].selected = !selected;
    this.setData({
      carts: carts
    });
    
    this.getTotalPrice();
  },

  /**
   * 删除购物车当前商品
   */
  deleteList(e) {
    console.log(e)
    // const index = e.currentTarget.dataset.index;
    // let carts = this.data.carts;
    // carts.splice(index, 1);
    // this.setData({
    //   carts: carts
    // });
    // if (!carts.length) {
    //   this.setData({
    //     hasList: false
    //   });
    // } else {
    //   this.getTotalPrice();
    // }
    var that = this
    wx.showModal({
      title: '',
      content: '确定要删除吗?',
      success:function(res){
        if (res.confirm) {
          wx.getStorage({
            key: 'openid',
            success: function (res) {
              let openid = res.data

              wx.request({
                url: app.globalData.URL + '/app/product/delAll?openid=' + openid + '&ids=' + e.currentTarget.id,
                success: function (res) {
                  console.log("删除", res)

                  if (res.data.flag == true) {
                    wx.showToast({
                      title: '删除成功',
                      duration:5000,
                      complete: function (res) { 
                        that.getCartList()
                      }
                    })  
                  }
                }

              })
            },
          })
        }
      }
    })
    
  },

  /**
   * 购物车全选事件
   */
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let carts = this.data.carts;

    for (let i = 0; i < carts.length; i++) {
      carts[i].selected = selectAllStatus;
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      carts: carts
    });
    
    this.getTotalPrice();
  },

  /**
   * 绑定加数量事件
   */
  addCount(e) {
    console.log(e)
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let num = carts[index].productNumber;
    num = num + 1;
    carts[index].productNumber = num;
    this.setData({
      carts: carts
    });

    console.log(this.data.carts)
    this.getTotalPrice();
  },

  /**
   * 绑定减数量事件
   */
  minusCount(e) {
    const index = e.currentTarget.dataset.index;
    const obj = e.currentTarget.dataset.obj;
    let carts = this.data.carts;
    let num = carts[index].productNumber;
    if (num <= 1) {
      return false;
    }
    num = num - 1;
    carts[index].productNumber = num;
    this.setData({
      carts: carts
    });
    this.getTotalPrice();
  },

  /**
   * 计算总价
   */
  getTotalPrice() {
    let carts = this.data.carts;                  // 获取购物车列表
    let total = 0;
    let ids = ''
    let number = ''
    for (let i = 0; i < carts.length; i++) {         // 循环列表得到每个数据
      if (carts[i].selected) {                     // 判断选中才会计算价格
        total += carts[i].productNumber * carts[i].productPrice;   // 所有价格加起来
        ids += carts[i].shoppingId + ',';
        number += carts[i].productNumber + ','
      }
    }
    console.log("ids,number", ids, number)

    this.setData({                                // 最后赋值到data中渲染到页面
      carts: carts,
      totalPrice: total,
      ids: ids,
      number : number
    });
  },

  shoppingList:function(){
    var that = this
    if(that.data.ids == ''){
      wx.showToast({
        title: '请选择商品',
        icon: '',
        image: '../../images/error.png'
      })
    }else{
      wx.navigateTo({
        url: '../shoppingList/shoppingList?ids=' + that.data.ids + '&number=' + that.data.number + '&money=' + that.data.totalPrice + '&isPro=0',
      })
    }
  }
})